from main import db
import datetime


class TagCategory(db.Model):
    __tablename__ = "category"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    category_name = db.Column(db.String(50), nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship('TagUser', backref='categories', foreign_keys=[user_id], lazy=True)

    create_at = db.Column(db.DateTime, default=datetime.datetime.now)

    def to_simple_map(self):
        return {
            "category_id": self.id,
            "category": self.category_name,
        }

    def to_all_map(self):
        tags_list = []
        for i in self.tags:
            tags_list.append(i.to_map())
        tags_list.reverse()
        return {
            "category_id": self.id,
            "category": self.category_name,
            "tags": tags_list
        }

    def to_map(self):
        return self.to_all_map()
